package com.jinwei.qunguanjia.common.utils;

import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.HttpResponseEnum;

import java.util.ArrayList;

/**
 * T代表VO的实体，S代表DTO的实体
 *
 * @author yuanzs
 * 2020/07/21 15:05
 */
public class PageDataUtil<S,T> {

    /**
     * 将page中的DTO转换为VO
     * @param tableDataInfoDTO page
     * @param clazz VO的class
     * @return PageInfoVO
     */
    public PageDataInfo<T> transform(PageDataInfo<S> tableDataInfoDTO, Class<T> clazz) {
        PageDataInfo<T> tableDataInfoVO = new PageDataInfo<>();
        if (null == tableDataInfoDTO) {
            tableDataInfoVO.setCode(HttpResponseEnum.SUCCESS.getCode());
            tableDataInfoVO.setMsg(HttpResponseEnum.SUCCESS.getDesc());
            tableDataInfoVO.setTotal(0);
            tableDataInfoVO.setData(new ArrayList<>());
            return tableDataInfoVO;
        }else{
            tableDataInfoVO.setCode(HttpResponseEnum.SUCCESS.getCode());
            tableDataInfoVO.setMsg(HttpResponseEnum.SUCCESS.getDesc());
            tableDataInfoVO.setTotal(tableDataInfoDTO.getTotal());
            tableDataInfoVO.setTotalPage(tableDataInfoDTO.getTotalPage());
            tableDataInfoVO.setData(JsonUtils.copyList(tableDataInfoDTO.getData(), clazz));
            return tableDataInfoVO;
        }
    }
}
